package com.zl.tcbnewcar.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.app.Fragment;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.zl.tcbnewcar.R;

import java.io.File;

/**
 * Created by zhanglei on 2017/4/14.
 * 图片加载类
 */

public class ImageLoader {

    public static void loadImageUrl(Activity activity, String url, ImageView imageView) {
        if (TextUtils.isEmpty(url)) return;
        String path = API.BASEURL + "/" + url;
        Glide.with(activity)
                .load(path)
                .placeholder(R.mipmap.icon_pro)
                .crossFade()
                .into(imageView);

    }

    public static void loadRoundImageUrl(final Activity activity, String url, final ImageView imageView) {
        Glide.with(activity).load(API.BASEURL + "/" + url)
                .asBitmap().placeholder(R.mipmap.icon_pro).centerCrop().into(new BitmapImageViewTarget(imageView) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(activity.getResources(), resource);
                circularBitmapDrawable.setCircular(true);
                imageView.setImageDrawable(circularBitmapDrawable);
            }
        });
    }

    public static void loadImageUrl(Fragment activity, String url, ImageView imageView) {
        if (TextUtils.isEmpty(url)) return;
        String path = API.BASEURL + "/" + url;
        Glide.with(activity)
                .load(path)
                .placeholder(R.mipmap.icon_pro)
                .crossFade()
                .into(imageView);
    }

    public static void loadImageUrl(Context activity, String url, ImageView imageView) {
        if (TextUtils.isEmpty(url)) return;
        String path = API.BASEURL + "/" + url;
        Glide.with(activity)
                .load(path)
                .placeholder(R.mipmap.icon_pro)
                .crossFade()
                .into(imageView);
    }

    /**
     * 加载本地图片
     *
     * @param imagePath
     * @param image
     */
    public static void loadImageFile(Activity activity, final String imagePath, final ImageView image) {
        //本地文件
        File file = new File(imagePath);
        Glide.with(activity).load(file).into(image);
    }

    /**
     * 加载本地图片
     *
     * @param imagePath
     * @param image
     */
    public static void loadImageFile(Context activity, final String imagePath, final ImageView image) {
        //本地文件
        File file = new File(imagePath);
        Glide.with(activity).load(file).into(image);
    }

}
